﻿using System;
using FluentNHibernate.Cfg;
using NHibernate;
using NHibernate.Tool.hbm2ddl;


namespace Family.Data.Factories
{
    public abstract class NHFactory
    {
        #region Fields
        
        protected readonly String configKey;

        #endregion

        #region Static properties

        public static ISessionFactory SessionFactory { get; set; }

        #endregion

        #region Hidden methods

        protected NHFactory(String configKey)
        {
            this.configKey = configKey;
        }

        protected virtual ISessionFactory CreateSessionfactory()
        {
            return GetConfiguration(configKey)
                //.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
                .BuildSessionFactory();
        }

        #endregion

        #region Public methods

        public void Init()
        {
            if (SessionFactory == null)
            {
                SessionFactory = CreateSessionfactory();
            }
        }

        public virtual ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

        public abstract FluentConfiguration GetConfiguration(String configKey);

        #endregion
    }
}
